﻿using DalSoft.Hosting.BackgroundQueue.DependencyInjection;
using MT.Common.Cache;
using MT.Common.QueueTask;
using MT.Component.Abstract.Aliyun;
using MT.Component.Concrete.Aliyun;
using MT.Component.CoreIoc;
using MT.DAL.Abstract;
using MT.DAL.Abstract.UserCenter;
using MT.DAL.Concrete;
using MT.DAL.Concrete.UserCenter;
using MT.WebApi.Core.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Linq;
using System.Reflection;
using Xbb.Common.Helpers;
using Xbb.Common.Helpers.Abstract;

namespace MT.WebApi.Core.ConfigureServices
{
    public static class DependencyResolverConfiguration
    {
        public static void DependencyResolverService(this IServiceCollection services)
        {
            services.AddHttpClient("location", config =>
            {
                config.BaseAddress = new System.Uri("https://apis.map.qq.com");
            });
            services.AddHttpClient();

            services.Configure<ApiBehaviorOptions>(options => options.SuppressModelStateInvalidFilter = true);

            Assembly[] assemblies = ReflectionHelper.GetAllAssembliesCoreWeb();
            Assembly repositoryAssemblies = assemblies.FirstOrDefault(x => x.FullName.Contains(".DAL"));
            services.AddAssemblyServices(repositoryAssemblies);
           // services.AddScoped<JwtSessionCache>();

            services.AddSingleton<ILock, DefaultRedisLock>();
            services.AddSingleton<IServiceConstructorParams, ServiceConstructorParams>();
            services.AddSingleton<ILock, DefaultRedisLock>();
            services.AddScoped<IFileService, FileService>(); 
            services.AddScoped<ITokenHelper, TokenHelper>();
            services.AddHttpContextAccessor();
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
          
            services.AddSingleton<ILock, DefaultRedisLock>();
            services.AddHostedService<QueuedHostedService>();
            services.AddSingleton<BackgroundPool>();
            services.AddSingleton<IBackgroundTaskQueue, BackgroundTaskQueue>();
            services.AddBackgroundQueue(maxConcurrentCount: 200, millisecondsToWaitBeforePickingUpTask: 2000,
            onException: exception =>
            {
            });
          


        }
    }
}
